package com.kgc.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.ssm.entity.Enquire;
import com.kgc.ssm.entity.EnquireExample;
import com.kgc.ssm.entity.Stock;
import com.kgc.ssm.entity.StockExample;
import com.kgc.ssm.entity.dto.Result;
import com.kgc.ssm.mapper.EnquireMapper;
import com.kgc.ssm.mapper.StockMapper;
import com.kgc.ssm.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 待审批采购计划处理service实现类
 */

@Service
@Transactional
public class StockServiceImpl implements StockService {

    @Autowired(required = false)
    private StockMapper stockMapper;

    @Autowired(required = false)
    private EnquireMapper enquireMapper;

    @Override
    public Result<List<Stock>> findPage(Integer pageNum, Integer pageSize) {
        //开始分页
        PageHelper.startPage(pageNum, pageSize);
        //设置条件
        StockExample example = new StockExample();
        StockExample.Criteria criteria = example.createCriteria();
        criteria.andStockStatusEqualTo("S001-20");

        //执行查询
        Page<Stock> page = (Page<Stock>) stockMapper.selectByExample(example);
        for (Stock stock : page.getResult()) {
            EnquireExample enquireExample = new EnquireExample();
            EnquireExample.Criteria enquireCriteria = enquireExample.createCriteria();
            enquireCriteria.andStockIdEqualTo(stock.getId());
            List<Enquire> enquiresList = enquireMapper.selectByExample(enquireExample);
            if (enquiresList != null && enquiresList.size() > 0) {
                Enquire obj = enquiresList.get(0);
                stock.setEnquireId(obj.getId());
                stock.setEnquireName(obj.getEnquireName());
            }
        }

        return new Result<>("查询成功", 200, page.getResult(), page.getTotal());
    }

    @Override
    public List<Map<String, Object>> findById(Integer id) {
        return stockMapper.findById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Stock row) {
        return stockMapper.updateByPrimaryKeySelective(row);
    }
}
